#include "stdafx.h"
#include "GUI.h"
#include "..\..\Global\globaldef.h"

Render::tResource* GUI::Components::Button::g_pTemplate = NULL;
Render::tResource* GUI::Components::Dialog::g_pTemplate = NULL;

GUI::Components::Button::Button() {

	if(GUI::Components::Button::g_pTemplate == NULL)
		Render::Singleton()->CreateTexture("GUI\\lv_button.png", &GUI::Components::Button::g_pTemplate);

	OnClickParam = NULL;
	OnClick = NULL;
}

GUI::Components::Button::~Button() {
	Release();
}

bool GUI::Components::Button::Create(const std::string& label) {

	try {

		m_label = label;

		RECT templateStamp;
		templateStamp.left = 0;
		templateStamp.top = 0;
		templateStamp.right = 60;
		templateStamp.bottom = 22;

		GUI::StampGetPercentage(&templateStamp, &m_templateStamp); 
		return true;

	}catch(tException e) {

		ZeroMemory(&m_templateStamp, sizeof(RECT));
		
		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::Components::Button::Release() {
	
	ZeroMemory(&m_templateStamp, sizeof(RECT));
	OnClickParam = NULL;
	OnClick = NULL;
	return true;
}

void GUI::Components::Button::OnMouseEvent(const float x, const float y, const MouseButton left, const MouseButton right) {

	float stampHeight = GUI::Height(&m_templateStamp);
	if(GUI::StampContainsPoint(&m_templateStamp, Position, x, y)) {

		switch(left) {

			case GUI::MouseButton::BUTTON_UP:
				
				if(m_templateStamp.top == 2 * stampHeight &&
					m_templateStamp.bottom == stampHeight * 3) {

					if(OnClick)
						OnClick(this, OnClickParam);
				}
				
				m_templateStamp.top = stampHeight;
				m_templateStamp.bottom = stampHeight * 2;

				break;

			case GUI::MouseButton::BUTTON_DOWN:
				m_templateStamp.top = stampHeight * 2;
				m_templateStamp.bottom = stampHeight * 3;
				break;
		}

	}
	else {

		m_templateStamp.top = 0;
		m_templateStamp.bottom = stampHeight;
	}
}

void GUI::Components::Button::Draw() {

	RECT r;
	GUI::StampGetPixel(&m_templateStamp, &r);
	Render::Singleton()->DrawSprite(GUI::Components::Button::g_pTemplate, Position, D3DXVECTOR2(1,1), &r);
	Render::Singleton()->DrawString(0, D3DCOLOR_ARGB(255,255,255,255), Position + D3DXVECTOR2(0.0075f, 0.0075f),(char*) m_label.c_str());
	
}

GUI::Components::Dialog::Dialog() {

	m_dispose = false;
	if(GUI::Components::Dialog::g_pTemplate == NULL)
		Render::Singleton()->CreateTexture("GUI\\lv_dialog.png", &GUI::Components::Dialog::g_pTemplate);

}

GUI::Components::Dialog::~Dialog() {
	Release();
}

bool GUI::Components::Dialog::Release() {

	FOREACH(GUI::Components::Button, i, m_Buttons)
		i->Release();

	m_Buttons.clear();
	m_title.clear();
	m_message.clear();
	return true;
}

bool GUI::Components::Dialog::Create(const std::string& title, const std::string& message, ComponentEvent OnOK, ComponentEvent OnCancel) {

	try {

		if(OnOK) {

			Button ok;
			ok.Create("OK");
			ok.Position = D3DXVECTOR2(20,20);
			ok.OnClick = OnOK;
			ok.OnClickParam = this;
			m_Buttons.push_back(ok);
		}

		if(OnCancel) {

			Button cancel;
			cancel.Create("Cancel");
			cancel.Position = D3DXVECTOR2(20,20);
			cancel.OnClick = OnCancel;
			cancel.OnClickParam = this;
			m_Buttons.push_back(cancel);
		}

		m_title = title;
		m_message = message;
		return true;

	}catch(tException e) {

		Release();
		return false;
	}
}

void GUI::Components::Dialog::Draw() {

	UINT w,h;
	Render::Singleton()->GetTextureInfo(GUI::Components::Dialog::g_pTemplate, &w, &h);

	w *= -0.5f;
	h *= -0.5f;
	D3DXVECTOR2 d;
	GUI::ScaleToWindow(w,h,&d);

	Render::Singleton()->DrawSprite(GUI::Components::Dialog::g_pTemplate, Position + d, D3DXVECTOR2(1,1), NULL);
	Render::Singleton()->DrawString(0, D3DCOLOR_ARGB(255,125,125,125), Position + d + D3DXVECTOR2(0.0075f, 0.0025f),(char*) m_title.c_str());
	Render::Singleton()->DrawString(0, D3DCOLOR_ARGB(255,255,255,255), Position + d + D3DXVECTOR2(0.0075f, 0.045f),(char*) m_message.c_str());
	
	d.y *= -1;
	UINT n = 0;
	FOREACH(GUI::Components::Button, i, m_Buttons) {

		D3DXVECTOR2 b;
		GUI::ScaleToWindow(2 + (60 * n++), -24, &b);
		i->Position = Position + d + b;
		i->Draw();
	}
}

void GUI::Components::Dialog::Update() {

}

void GUI::Components::Dialog::OnMouseEvent(const float x, const float y, const GUI::MouseButton left, const GUI::MouseButton right) {

	FOREACH(GUI::Components::Button, i, m_Buttons)
		i->OnMouseEvent(x,y,left,right);
}

void GUI::Components::Dialog::OnClose(void* pCaller, void* pParam) {

	Components::Dialog* pDialog = (Components::Dialog*)pParam;
	if(pDialog) {

		for(uint i = 0; i < GUI::g_Dialogs.size(); i++)
			if(GUI::g_Dialogs[i] == pDialog) {

				pDialog->m_dispose = true;
				break;
			}
	}
}
